package com.suxin.plugin;

import com.alibaba.fastjson.JSON;
import com.suxin.web.TestController;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.Arrays;

/**
 * @author Tang
 * @classname TestMain
 * @description [  ]
 * @date 2022/6/16 23:42
 */
public class TestMain {

    @Test
    public void test01() throws Exception {
        String fileName = "/Users/tanglonglong/Desktop/attribute-stock-2.2.1-RELEASE-jar-with-dependencies.jar";

        URL url = new File(fileName).toURI().toURL();

        URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[]{url}, TestMain.class.getClassLoader());

        PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(urlClassLoader);
        Resource[] resources = patternResolver.getResources("classpath*:com/jsh/erp/plugin/**/*.class");

        for (Resource resource : resources) {
            System.out.println(resource);
        }

    }

    public static void main(String[] args) throws Exception {
        System.out.println(Paths.get("tang").resolve("11"));
    }

    @Test
    public void test02() throws Exception{
        ClassPathResource pathResource = new ClassPathResource("application.yml");
        System.out.println(pathResource.getURL().toURI());
    }

    @Test
    public void test03() throws Exception{
        RequestMapping requestMapping = TestController.class.getAnnotation(RequestMapping.class);
        // 这样获取不到注解的值
        System.out.println(Arrays.toString(requestMapping.path()));
        // 注解的本质是 AnnotationInvocationHandler 实例
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(requestMapping);
        System.out.println(invocationHandler);
        // 反射获取 Map<String, Object> memberValues，
        Field field = invocationHandler.getClass().getDeclaredField("memberValues");
        field.setAccessible(true);
        // 这样获取值
        System.out.println(JSON.toJSONString(field.get(invocationHandler)));
    }

}